package com.aurora.oa.work.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.MainFragmentPageAdapter;
import com.aurora.oa.home.view.HomePageFragment;
import com.aurora.oa.message.view.MessageFragment;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.mycenter.view.MyCenterFragment;
import com.aurora.oa.signin.view.SigninMainFragment;
import com.aurora.oa.signin.view.TrackActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/30 0030.
 */
public class WorkLogMainActivity extends BaseActivity{
    List<Fragment> mfragments = new ArrayList<>();;
    private List<String> titles = new ArrayList<>();
    ViewPager mviewpager;
    TabLayout tabs;
    RequestHttpModel model = new RequestHttpModelImpl();
    private int[] tabIcons = {
            R.mipmap.ico_write,
            R.mipmap.ico_eye,
    };

    private int[] tabIconsPressed ={
            R.mipmap.ico_write_h,
            R.mipmap.ico_eye_h,
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_work_log_main);
        initTitlebar(true,"日志",true);
        initView();
        initData();
    }

    private void initView() {
        mviewpager = (ViewPager) findViewById(R.id.mviewpager);
        tabs = (TabLayout) findViewById(R.id.tabs);
    }

    private void initData() {
        titles.add("写日志");
        titles.add("看日志");
        mfragments.add(new WriteLogFragment());
        mfragments.add(new SeeLogFragment());
        MainFragmentPageAdapter adapter = new MainFragmentPageAdapter(getSupportFragmentManager(),mfragments,titles);
        mviewpager.setAdapter(adapter);
        tabs.setupWithViewPager(mviewpager);
        setupTabIcons();

        tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setTabSelected(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                setTabUnselected(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }





    private void setupTabIcons() {
        for(int i=0;i<titles.size();i++){
            TabLayout.Tab tab = tabs.getTabAt(i);
            tab.setCustomView(getTabView(i));
        }
    }

    private View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab,null);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        ImageView img_title =(ImageView) view.findViewById(R.id.img_title);
        tv_title.setText(titles.get(position));
        img_title.setImageResource(tabIcons[position]);
        tv_title.setTextColor(getResources().getColor(R.color.tab_unselected_gray));
        view.setPadding(5,5,5,5);
        if(position==0){
            tv_title.setTextColor(getResources().getColor(R.color.tab_selected_blue));
            img_title.setImageResource(tabIconsPressed[position]);
        }
        return view;
    }


    private void setTabSelected(TabLayout.Tab tab) {

        View view = tab.getCustomView();
        TextView tv = (TextView) view.findViewById(R.id.tv_title);
        // tv.setTextColor(Color.parseColor("#6f7171"));
        tv.setTextColor(getResources().getColor(R.color.tab_selected_blue));
        ImageView iv =(ImageView) view.findViewById(R.id.img_title);
        iv.setImageResource(tabIconsPressed[tab.getPosition()]);
    }

    private void setTabUnselected(TabLayout.Tab tab){
        View view = tab.getCustomView();
        TextView tv = (TextView) view.findViewById(R.id.tv_title);
        // tv.setTextColor(Color.parseColor("#6f7171"));
        tv.setTextColor(getResources().getColor(R.color.tab_unselected_gray));
        ImageView iv =(ImageView) view.findViewById(R.id.img_title);
        iv.setImageResource(tabIcons[tab.getPosition()]);
    }

}
